# -*- coding: UTF-8 -*-
import re
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django import forms

class RegistrationForms(forms.Form):
    username = forms.CharField(label='用户名', max_length=30,help_text="Please enter you name!")
    email = forms.EmailField(label='电子邮箱')
    password1 = forms.CharField(
        label='密码',
        widget=forms.PasswordInput()
    )
    password2 = forms.CharField(
        label='密码 (再次输入)',
        widget=forms.PasswordInput()
    )

    def clean_password2(self):
        if 'password1' in self.cleaned_data:
            password1 = self.cleaned_data['password1']
            password2 = self.cleaned_data['password2']
        if password1 == password2:
            return password2
        raise forms.ValidationError('密码不匹配！')

    def clean_username(self):
        username = self.cleaned_data['username']
        if not re.search(r'^\w+$', username):
            raise forms.ValidationError('错误，用户名只能包含字母和下划线！')
        try:
            User.objects.get(username=username)
        except ObjectDoesNotExist:
            return username
        raise forms.ValidationError('错误，用户名已经存在！')
